<?php

namespace bc\model\order;

use bc\ar\MActiveRecord;
use bc\model\OrderModel;
use bc\model\ProductModel;

class DetailModel extends MActiveRecord
{
	
	/**
	 * 
	 * @var string
	 */
	public $autoId = 'order_detail_id';
	
	
	/**
	 * 
	 */
	public static function tableName()
	{
		return '{{%order_detail}}';
	}
	
	
	/**
	 * 
	 */
	public function getProductTableName()
	{
		return ProductModel::getTableSchema()->name;
	}
	
	/**
	 * 
	 */
	public function getOrderTableName()
	{
		return OrderModel::getTableSchema()->name;
	}
	
	
	/**
	 * 通过order联表查询
	 * 
	 * @param array|string $params        	
	 * @param integer $page        	
	 * @param integer $pageSize        	
	 * @param string $oderBy        	
	 */
	public function getProductByOrder($params, $page = null, $pageSize = null, $oderBy = null)
	{
		$ar = $this->find ()->select ( $this->columns )
			->leftJoin ( $this->getOrderTableName (), self::getTableSchema ()->name . '.order_id=' . $this->getOrderTableName () . '.order_id' )
			->leftJoin ( $this->getProductTableName(), self::getTableSchema ()->name . '.product_id=' . $this->getProductTableName () . '.product_id' );
		if ($params)
		{
			$ar->where ( $params );
		}
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset ( $offset )->limit ( $pageSize );
		}
		
		if ($this->sort != null)
			$ar->orderBy ( $oderBy );
		
		return $ar->asArray ()->all ();
	}
	
	
	
}

